package edu.hcmus.sow.web.user;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.validation.Errors;

import edu.hcmus.sow.dao.UserDAO;
import edu.hcmus.sow.domain.User;
import edu.hcmus.sow.utils.SecurityUtil;

public class UserForm {
   private Integer id;
   @NotEmpty(message = "Username must not be blank.")
   private String username;
   private String password;
   private Boolean enabled;
   @NotEmpty(message = "First Name must not be blank.")
   private String firstName;
   @NotEmpty(message = "Last Name must not be blank.")
   private String lastName;
   private String middleName;
   private String email;
   private String phone;
   private int role;

   public UserForm() {

   }

   public UserForm(Integer id, String username, String password, Boolean enabled) {
      this.id = id;
      this.username = username;
      this.password = password;
      this.enabled = enabled;
   }

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public Boolean getEnabled() {
      return enabled;
   }

   public void setEnabled(Boolean enabled) {
      this.enabled = enabled;
   }

   public String getFirstName() {
      return firstName;
   }

   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }

   public String getLastName() {
      return lastName;
   }

   public void setLastName(String lastName) {
      this.lastName = lastName;
   }

   public String getMiddleName() {
      return middleName;
   }

   public void setMiddleName(String middleName) {
      this.middleName = middleName;
   }

   public String getEmail() {
      return email;
   }

   public void setEmail(String email) {
      this.email = email;
   }

   public String getPhone() {
      return phone;
   }

   public void setPhone(String phone) {
      this.phone = phone;
   }

   public int getRole() {
      return role;
   }

   public void setRole(int role) {
      this.role = role;
   }

   public void validate(Errors errors, UserDAO userDAO) {
      if (!errors.hasFieldErrors("username")) {
         List<User> users = userDAO.findUserByUsername(SecurityUtil.getCurrentClient(), username);
         int userId = (id == null) ? -1 : id.intValue();
         if (users.size() > 0 && users.get(0).getId().intValue() != userId) {
            errors.rejectValue("username", "err.username", "Username already exists.");
         }
      }

      if (id == null && StringUtils.isEmpty(password)) {
         errors.rejectValue("password", "err.password", "Password must not be blank.");
      }
   }
}
